package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/clazzs")
public class ClazzsController {
    @Autowired
    private ClazzService clazzService;

    /*
    * 班级列表分页查询
    * */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        log.info("班级分页查询：{}",clazzQueryParam);
        PageResult<Clazz> pageResult=clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 新增班级
     */
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz){
        log.info("新增班级：{}",clazz);
        clazzService.addClazz(clazz);
        return Result.success();
    }

    /**
     * 更新班级信息 先回显 再编辑
     */
    @GetMapping("{id}")
    public Result findById(@PathVariable Integer id){
        log.info("根据id查询：{}",id);
        Clazz clazz=clazzService.findById(id);
        return Result.success(clazz);
    }
    /**
     * 更新班级信息 开始编辑
     */
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz){
        clazzService.update(clazz);
        return Result.success();
    }
    /**
     * 删除班级
     */
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id){
        clazzService.delete(id);
        return Result.success();
    }
    /**
     * 查询所有的班级
     */
    @GetMapping("/list")
    public Result findAllClazz(){
       List<Clazz> clazzs= clazzService.findAll();
       return Result.success(clazzs);
    }

}
